﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

using LeMigue.TeamPortal.Base.Mvc;
using LeMigue.TeamPortal.Base.View;

namespace LeMigue.TeamPortal.Base.Helpers
{
    public static class MainMenuHelper
    {
        public static void DisplayMenu(this HtmlHelper helper, string viewTemplate)
        {
            MainMenu[] containerHome = TeamPortalPlugins.ConteinerPlugin<MainMenu>().OrderBy(t => t.Ordem).ToArray();

            IList<MainMenuView> menuView = new List<MainMenuView>();

            foreach (MainMenu box in containerHome)
            {
                bool isAtivo = MenuAtivo(helper.ViewContext, box);
                menuView.Add(new MainMenuView(box, isAtivo));
            }

            helper.RenderPartial(viewTemplate, menuView);
        }

        private static bool MenuAtivo(ViewContext viewContext, MainMenu mainMenuBox)
        {
            if(viewContext.IsChildAction)
                return MenuAtivo(viewContext.ParentActionViewContext, mainMenuBox);

            if(mainMenuBox.Controller == viewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString() &&
                mainMenuBox.Action == viewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString())
                return true;

            return false;
        }
    }
}
